package org.jeecgframework.web.tcdev.service.impl;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.tcdev.dao.ad.AdDao;
import org.jeecgframework.web.tcdev.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by chenghao on 2015/4/9.
 */
@Service("adService")
@Transactional
public class AdServiceImpl extends CommonServiceImpl implements AdService {
    @Autowired
    private AdDao adDao;

    @Override
    public List<Map<String, Object>> listAll(int pid, int page, int rows) {
        return adDao.listAll(pid, page, rows);
    }

    @Override
    public Integer getCount(int pid) {
        Object count = findOneForJdbc("select count(1) as count from t_ad_info where position_id=" + pid).get("count");
        if (count == null) {
            return 0;
        } else {
            return Integer.valueOf(String.valueOf(count));
        }
    }

    @Override
    public List<Map<String, Object>> adPositionListAll(int page, int rows) {
        String sql = "select p.id, p.name, p.code,IFNULL(p.type,'') AS 'type',IFNULL(p.templet,'') AS templet,IFNULL(p.sort,'') AS sort, case when p.page is null then '' else p.page end as page from t_ad_position as p";

        List<Map<String, Object>> lists = findForJdbc(sql,page,rows);

        return lists;
    }

    @Override
    public Integer getPositionCount() {
        Object count = findOneForJdbc("select count(1) as count from t_ad_position").get("count");
        if (count == null) {
            return 0;
        } else {
            return Integer.valueOf(String.valueOf(count));
        }
    }

    @Override
    public Map<String, Object> getAdPosition(int id) {
        String sql = "select p.id, " +
                "IFNULL(p.type,'') AS 'type',IFNULL(p.templet,'') AS templet,IFNULL(p.sort,'') AS sort, "+
                "case when p.name is null then '' else p.name end as name, " +
                "case when p.code is null then '' else p.code end code, " +
                "case when p.page is null then '' else p.page end as page " +
                "from t_ad_position as p where p.id=?";

        return findOneForJdbc(sql, id);
    }

    @Override
    public void saveUpdateAdPosition(Map<String, Object> map) {
        Object pid = map.get("id");
        if (StringUtil.isNotEmpty(pid)) { //不为空
            executeSql("update t_ad_position set name=:name, code=:code, page=:page,type=:type,templet=:templet,sort=:sort where id=:id", map);
        } else {
            Object rid = executeSqlReturnKey("insert into t_ad_position(name, code, page,type,templet,sort) values(:name, :code, :page,:type,:templet,:sort)", map);
            executeSql("update t_ad_info set position_id=? where id=?", rid, map.get("adId"));
        }
    }

    @Override
    public Map<String, Object> getAdInfoById(int id) {
        String sql = "select id, name, pic, case when url is null then '' else url end as url, show_index as showIndex, enable from t_ad_info where id=?";
        return findOneForJdbc(sql, id);
    }

    @Override
    public void saveAd(Map<String, Object> map) {
        executeSql("insert into t_ad_info(name, pic, url, show_index, enable, position_id) values(:name, :picPath, :url, :showIndex, :enable, :pid)", map);
    }

    @Override
    public void updateAd(Map<String, Object> map) {
        executeSql("update t_ad_info set name=:name, pic=:picPath,url=:url,show_index=:showIndex,enable=:enable, position_id=:pid where id=:id", map);
    }

    @Override
    public void delAdPosition(int id) {
        executeSql("delete from t_ad_position where id=?", id);
    }

    @Override
    public void delAdInfo(int id) {
        executeSql("delete from t_ad_info where id=?", id);
    }


}
